package com.valveinspectionsys.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;

/**
 * 图片压缩
 * @author King
 *
 */
public class CompressUtil {

    public static final int WIDTH_SIZE = 600;
    
    public static final int HIGTH_SIZE=800;
    
    public static Bitmap compressPic(String path){
	 BitmapFactory.Options opts = new BitmapFactory.Options();
	        //设为true，不为图片非配内存（不加载到内存中），仅获取图片原始大小,所以返回值为NULL
	        opts.inJustDecodeBounds = true;
	        BitmapFactory.decodeFile(path, opts);
	        int srcWidth = opts.outWidth;
	        int srcHeight = opts.outHeight;
	        System.out.println("----srcwidth---------------"+srcWidth);
	        System.out.println("-----srcheight--------------"+srcHeight);
	        double ratio;
	        int destWidth;
	        int destHeight;
	        if (srcWidth > srcHeight) {
	            destWidth = WIDTH_SIZE;
	            ratio = (double)srcWidth / WIDTH_SIZE;
	            destHeight = (int)(srcHeight / ratio);
	        } else {
	            destHeight = HIGTH_SIZE;
	            ratio = (double)srcHeight / HIGTH_SIZE;
	            destWidth = (int)(srcWidth / ratio);
	        }
               
	        opts.inJustDecodeBounds = false;
	        //缩放放图片大小以此缩放比例值为准，outWidth与outHeight未必准确
	        opts.inSampleSize = (int)ratio + 1;
	        opts.outWidth = WIDTH_SIZE;
	        opts.outHeight = HIGTH_SIZE;
	        Matrix matrix=new Matrix();
	        matrix.postRotate(90);
	        Bitmap newPic = BitmapFactory.decodeFile(path, opts);
	        newPic = Bitmap.createBitmap(newPic, 0, 0, newPic.getWidth(), newPic.getHeight(), matrix, true);
	       

	return newPic;
	
    } 

          
}
